import { useApprovalProvide } from '../..';
import { isApprovalTaskDone } from '../components/Progress/list/item/TaskNode';

/**
 * 审批节点信息
 */
export const useNode = () => {
  const { progressData, isApprovalUser } = useApprovalProvide();

  /**
   * 重新发起的模板审批 第一个节点
   */
  const isTemplateFirstNode = computed(() => {
    const activityDefinitionIds = ['UserTask_LY_template_02', 'UserTask_501_5001_01'];
    if (!isApprovalUser.value) return -1;

    const index = progressData.value?.taskActivities?.findIndex((task) => {
      if (!activityDefinitionIds.includes(task.activityDefinitionId + '')) return false;
      // if (task.activityDefinitionId !== activityDefinitionId) return false;

      return task.userTasks?.some((userTask) => {
        // 没有审批完成的节点
        return !isApprovalTaskDone(userTask.approvalStatus, userTask?.endTime);
      });
    });

    return index === 0;
  });

  return {
    isTemplateFirstNode
  };
};
